﻿namespace Acoris.Media
{
    using System.Diagnostics;

#if WIN2D && NET_UWP
    using Microsoft.Graphics.Canvas;
    using Microsoft.Graphics.Canvas.Brushes;
#endif  

    /// <summary>
    /// 
    /// </summary>
    public sealed class BrushConverter:IBrushConverter
    {
#if WIN2D
        public BrushConverter(ICanvasResourceCreator device)
        {
            this.Device = device;
        }
#endif

        /// <summary>
        /// 
        /// </summary>
#if WIN2D
        public ICanvasResourceCreator Device { get; private set; }
#endif

        /// <summary>
        /// 
        /// </summary>
        /// <param name="brush"></param>
        /// <returns></returns>
#if WIN2D
        public ICanvasBrush Convert(Brush brush)
        {
            Debug.Assert(Device != null);

            if(brush is SolidColorBrush scb) {
                if (!scb.CachedResult) {
                    scb.CachedBrush =  new CanvasSolidColorBrush(Device,GetColor(scb.m_color));
                }
                return scb.CachedBrush;
            }
            return null;
        }
#else
        public void Convert(Brush brush)
        {
        }
#endif

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
#if WIN2D && NET_UWP
        public Windows.UI.Color GetColor(Color color)
        {
            return Windows.UI.Color.FromArgb(color.A, color.R, color.G, color.B);
        }
#endif
    }
}
